<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity">

  <!-- DEPLOYMENT INSERT -->

  <insert id="insertDeployment" parameterType="org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity">
    insert into ${prefix}ACT_RE_DEPLOYMENT(ID_, NAME_, DEPLOY_TIME_, SOURCE_, TENANT_ID_)
    values(#{id, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{deploymentTime, jdbcType=TIMESTAMP}, #{source, jdbcType=VARCHAR}, #{tenantId, jdbcType=VARCHAR})
  </insert>

  <!-- DEPLOYMENT UPDATE -->

  <!-- DEPLOYMENT DELETE -->

  <delete id="deleteDeployment" parameterType="string">
    delete from ${prefix}ACT_RE_DEPLOYMENT where ID_ = #{id}
  </delete>

  <!-- DEPLOYMENT RESULTMAP -->

  <resultMap id="deploymentResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="name" column="NAME_" jdbcType="VARCHAR" />
    <result property="source" column="SOURCE_" jdbcType="VARCHAR" />
    <result property="deploymentTime" column="DEPLOY_TIME_" jdbcType="TIMESTAMP"/>
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR" />
  </resultMap>

  <!-- DEPLOYMENT SELECT -->

  <select id="selectDeployment" parameterType="string" resultMap="deploymentResultMap">
    select * from ${prefix}ACT_RE_DEPLOYMENT where ID_ = #{id}
  </select>

  <select id="selectDeploymentsByIds" resultMap="deploymentResultMap">
    select
          *
    from
          ${prefix}ACT_RE_DEPLOYMENT
    <if test="parameter != null">
    where
          ID_ in
            <foreach item="id"
                     collection="parameter"
                     open="("
                     separator=","
                     close=")">
              #{id}
            </foreach>
    </if>

  </select>

  <select id="selectDeploymentsByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.DeploymentQueryImpl" resultMap="deploymentResultMap">
  	<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="selectDeploymentsByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <select id="selectDeploymentCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.DeploymentQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectDeploymentsByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>

  <sql id="selectDeploymentsByQueryCriteriaSql">

    from ${prefix}ACT_RE_DEPLOYMENT RES

    <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoin" /> 

    <where>
      <if test="deploymentId != null">
        RES.ID_ = #{deploymentId}
      </if>
      <if test="name != null">
        and RES.NAME_ = #{name}
      </if>
      <if test="nameLike != null">
        and RES.NAME_ like #{nameLike} ESCAPE ${escapeChar}
      </if>
      <if test="deploymentBefore != null">
        and RES.DEPLOY_TIME_ &lt; #{deploymentBefore}
      </if>
      <if test="deploymentAfter != null">
        and RES.DEPLOY_TIME_ &gt; #{deploymentAfter}
      </if>
      <if test="sourceQueryParamEnabled">
        <if test="source != null">
          and RES.SOURCE_ = #{source}
        </if>
        <if test="source == null">
          and RES.SOURCE_ is null
        </if>
      </if>
      <if test="isTenantIdSet">
        <if test="tenantIds != null &amp;&amp; tenantIds.length > 0">
          and ( RES.TENANT_ID_ in
          <foreach item="tenantId" index="index" collection="tenantIds"
                   open="(" separator="," close=")">
            #{tenantId}
          </foreach>
          <if test="includeDeploymentsWithoutTenantId">
            or RES.TENANT_ID_ is null 
          </if>
          )
        </if>
        <if test="tenantIds == null">
          and RES.TENANT_ID_ is null
        </if>
      </if>

      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck" />
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck" />

    </where>
  </sql>

  <select id="selectDeploymentsByName" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="deploymentResultMap">
    select * from ${prefix}ACT_RE_DEPLOYMENT D where NAME_=#{parameter, jdbcType=VARCHAR} order by D.DEPLOY_TIME_ desc
  </select>

  <!-- mysql specific -->
  <select id="selectDeploymentsByQueryCriteria_mysql" parameterType="org.camunda.bpm.engine.impl.DeploymentQueryImpl" resultMap="deploymentResultMap">
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="selectDeploymentsByQueryCriteriaSql"/>
   	${orderBy}
    ${limitAfter}
  </select>

  <!-- mysql specific -->
  <select id="selectDeploymentCountByQueryCriteria_mysql" parameterType="org.camunda.bpm.engine.impl.DeploymentQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectDeploymentsByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>

  <select id="selectDeploymentIdsByProcessInstances" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultType="string">
    select distinct P.DEPLOYMENT_ID_
    from ${prefix}ACT_RU_EXECUTION RES
    inner join ${prefix}ACT_RE_PROCDEF P
    on RES.PROC_DEF_ID_ = P.ID_
    WHERE
      RES.PARENT_ID_ is null and
      <bind name="listOfIds" value="parameter" />
      <bind name="fieldName" value="'RES.PROC_INST_ID_'" />
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.applyInForPaginatedCollection" />
  </select> 

</mapper>